package com.backend.modules.system.controller;

import com.backend.common.model.R;
import com.backend.common.utils.ExcelUtils.ImportResult;
import com.backend.modules.system.convert.UserConvert;
import com.backend.modules.system.dto.ClassStudentQueryDTO;
import com.backend.modules.system.dto.StudentDTO;
import com.backend.modules.system.dto.StudentExcelDTO;
import com.backend.modules.system.service.TeacherClassService;
import com.backend.modules.system.vo.ClassTreeVO;
import com.backend.modules.system.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "教师班级管理", description = "教师班级管理相关接口")
@RestController
@RequestMapping("/v1/teacher/classes")
@RequiredArgsConstructor
@PreAuthorize("hasRole('TEACHER')")
public class TeacherClassController {

    private final TeacherClassService teacherClassService;

    @Operation(summary = "获取教师负责的班级树")
    @GetMapping("/tree")
    public R<List<ClassTreeVO>> getClassTree() {
        return R.ok(teacherClassService.getTeacherClassTree());
    }

    @Operation(summary = "获取班级学生列表")
    @GetMapping("/{classId}/students")
    public R<Page<UserVO>> getClassStudents(
            @Parameter(description = "班级ID") @PathVariable Long classId,
            @ParameterObject ClassStudentQueryDTO query,
            @ParameterObject Pageable pageable) {
        return R.ok(teacherClassService.getClassStudents(classId, query, pageable)
                .map(UserConvert::convert));
    }

    @Operation(summary = "创建班级学生")
    @PostMapping("/{classId}/students")
    public R<Void> createStudent(
            @Parameter(description = "班级ID") @PathVariable Long classId,
            @Validated @RequestBody StudentDTO dto) {
        teacherClassService.createStudent(classId, dto);
        return R.ok();
    }

    @Operation(summary = "更新班级学生")
    @PutMapping("/{classId}/students/{studentId}")
    public R<Void> updateStudent(
            @Parameter(description = "班级ID") @PathVariable Long classId,
            @Parameter(description = "学生ID") @PathVariable Long studentId,
            @Validated @RequestBody StudentDTO dto) {
        teacherClassService.updateStudent(classId, studentId, dto);
        return R.ok();
    }

    @Operation(summary = "删除班级学生")
    @DeleteMapping("/{classId}/students/{studentId}")
    public R<Void> deleteStudent(
            @Parameter(description = "班级ID") @PathVariable Long classId,
            @Parameter(description = "学生ID") @PathVariable Long studentId) {
        teacherClassService.deleteStudent(classId, studentId);
        return R.ok();
    }

    @Operation(summary = "重置学生密码")
    @PutMapping("/{classId}/students/{studentId}/password/reset")
    public R<Void> resetStudentPassword(
            @Parameter(description = "班级ID") @PathVariable Long classId,
            @Parameter(description = "学生ID") @PathVariable Long studentId) {
        teacherClassService.resetStudentPassword(classId, studentId);
        return R.ok();
    }

    @Operation(summary = "更新学生状态")
    @PutMapping("/{classId}/students/{studentId}/status")
    public R<Void> updateStudentStatus(
            @Parameter(description = "班级ID") @PathVariable Long classId,
            @Parameter(description = "学生ID") @PathVariable Long studentId,
            @Schema(description = "状态(true:启用,false:禁用)") @RequestParam Boolean status) {
        teacherClassService.updateStudentStatus(classId, studentId, status);
        return R.ok();
    }

    @Operation(summary = "导出班级学生")
    @GetMapping("/{classId}/students/export")
    public ResponseEntity<byte[]> exportStudents(
            @Parameter(description = "班级ID") @PathVariable Long classId) {
        return teacherClassService.exportStudents(classId);
    }

    @Operation(summary = "导入班级学生")
    @PostMapping("/{classId}/students/import")
    public R<ImportResult<StudentExcelDTO>> importStudents(
            @Parameter(description = "班级ID") @PathVariable Long classId,
            @Parameter(description = "Excel文件") @RequestParam("file") MultipartFile file) {
        return R.ok(teacherClassService.importStudents(classId, file));
    }

    @Operation(summary = "下载学生导入模板")
    @GetMapping("/students/template")
    public ResponseEntity<byte[]> downloadStudentTemplate() {
        return teacherClassService.downloadStudentTemplate();
    }
} 